<?php 
session_start();
require('includes/eshop_fns.php');

$product_type_id = $_REQUEST['product_type_id'];
$product_name = $_REQUEST['product_name'];

$conn = db_connect();
$sqls = 'SELECT `product_type_id`,`product_type_name` FROM `product_type` ORDER BY `product_type_order`';
//$sql2 = 'SELECT `product_id`,`product_name` FROM `product` WHERE `product_type_id` = :id AND `product_status` = 1 ';

if(is_numeric($product_type_id) && $product_type_id != 0){
	$sql = 'SELECT `product_type_id`,`product_type_name` FROM `product_type` WHERE `product_type_id` = :type_id ORDER BY `product_type_order`';
	$stmt = $conn -> prepare($sql);
	$stmt -> bindParam(':type_id',$product_type_id);
}else{
	
	$sql = $sqls;
	$stmt = $conn -> prepare($sql);
	
	
}
$stmt -> execute();
$vo = $stmt -> fetchAll();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>产品列表</title>
</head>

<body>
<table width="802" border="1" align="center">
<form name="loginform" action="login.php?act=login" method="post" />
	<tr>
		<td align="right">
		<?php 
		if (!isset($_SESSION['customer_name'])){
		?>
		用户名：<input name="loginname" /> 密码：<input name="loginpwd" /> <input name="submit" type="submit" value="登陆" />
		<?php 
		}else{
			echo '欢迎您：'.$_SESSION['customer_name'].'！&nbsp;<a href="customerManager.php">[个人中心]</a> &nbsp;<a href="cart.php">[我的购物车]</a>&nbsp;<a href="login.php?act=out">[退出]</a>';
		}
		?></td>
	</tr>
</form>
</table>
<table width="802" border="1" align="center">
<form name="searchform" action="" method="post" />
  <tr>
    <td width="115">产品关键字</td>
    <td width="194"><input name="product_name" type="text" value="<?php echo $product_name;?>" /></td>
    <td width="193"><input type="submit" value="搜索"/></td>
    </form>
    <form name="searchform1" action="" method="post" />
    <td width="199">产品类别</td>
    <td width="151"><?php select_out('所有分类',0,$sqls,'product_type_id','product_type_id','product_type_name',$product_type_id) ?></td>
    <td width="111"><input type="submit" value="查询"/></td>
  </tr>
</form>
</table>
<table width="800" border="1" align="center">
  <?php 
  //var_dump($vo);
  foreach ($vo as $pType){
  	//if(strlen($product_name) > 0){
  		$product_name = "%$product_name%";
		$sql2 = "SELECT `product_id`,`product_name` FROM `product` WHERE `product_name` like :name  AND `product_type_id` = :id AND `product_status` = 1 ";
		$stmt2 = $conn -> prepare($sql2);
		$stmt2 -> bindParam(':name',$product_name);
		
	//}else{
		//$sql2 = 'SELECT `product_id`,`product_name` FROM `product` WHERE `product_type_id` = :id ';
  		//$stmt2 = $conn -> prepare($sql2);
	//}
  	$stmt2 -> bindParam(':id',$pType['product_type_id']);
	$stmt2 -> execute();
	$vo2 = $stmt2 -> fetchAll();
	$row = count($vo2);
  ?>
  <tr>
    <td width="229"><?php echo $pType['product_type_name']; ?></td>
    <td width="555">
    <table border="1" width="100%">
    	<tr>
    <?php 
    	$i = 0;
    	foreach ($vo2 as $pName){
			$i += 1;
			//for($i = 0;$i < $row;$i++){
    ?>
    <td width="33%" align="center" height="80"><a href="productShow.php?id=<?php echo $pName['product_id'] ?>" target="_blank"><?php echo $pName['product_name'];?></a><br />[<a href="cart.php?id=<?php echo $pName['product_id'] ?>&act=add" target="_blank"><font size="-1">加入购物车</font></a>]</td>
    <?php 
    			if(fmod($i,3) == 0){
    				if($i != $row){
						echo '</tr><tr>';
    				}
    			}else{
    				if($i == $row){
    	    			for($k = 0;$k < 3-(fmod($row,3));$k++){
    	        			echo "<td width='33%'>&nbsp;</td>";
    	        		}
    	    		}
    			}
			//}
    	}
    ?></tr>
    </table>
    </td>
  </tr>
  <?php 
  }
  ?>
  
</table>


</body>
</html>
